from Meta import Meta
from Person import Person
import time


def main():
    world = Meta(20000, 100, 1000, 2000, 100)
    world.setPatient0(1)
    # world.allMask(0.5)
    world.setConfront(True)
    #world.vaccinate(0.9, 0.8)
    while True:
        dist = world.run1day()
        # print(dist)
        # print(dist[0])
        # print('=======')
        # print(dist[1])
        # print('=======')
        # print(dist[2])
        # print('=======')
        # world.errorCheck()
        print('# day {}'.format(world.getDays()))
        health, hide, no_sym, patient, dead, person_total = world.getPersonStatus()
        print('  人数统计：  健康({})  潜伏({})  无症状({})  病发({})  死亡({})'.format(health, hide, no_sym, patient, dead))
        total, remain = world.getHospitalCapacity()
        print('  医院：  剩余({})  总({})'.format(remain, total))
        total, remain = world.getIsolationCapacity()
        print('  隔离所：  剩余({})  总({})'.format(remain, total))
        # if day == 1:
        #     world.vaccinate(0.78, 0.5)
        if hide + no_sym + patient == 0:
            break
        # time.sleep(1)
    print(world.getPatientCum())


def test():
    people = []
    num = 10000
    for i in range(num):
        people.append(Person(0))
        people[i].testInfected(anti=0.)
    patient = [0 for i in range(num)]
    pat_day = [0 for i in range(num)]
    hea_day = [0 for i in range(num)]
    no_sym = [0 for i in range(num)]
    status = [0 for i in range(5)]
    day = 0
    sta = time.time()
    while True:
        day += 1
        for i in range(num):
            # 医院
            # cure_rate=0.4, dead_rate=0.006
            # 隔离点
            # cure_rate=0.2, dead_rate=0.01
            # else
            # cure_rate=0.1, dead_rate=0.03
            people[i].defender(cure_rate=0.36, dead_rate=0.006)
            if people[i].getStatus() == 3 and patient[i] == 0:
                patient[i] = 1
                pat_day[i] = day
            elif people[i].getStatus() == 2:
                no_sym[i] = 1
            elif people[i].getStatus() == 0:
                hea_day[i] += 1
        status = [0 for i in range(5)]
        for i in range(num):
            status[people[i].getStatus()] += 1
        if status[0] + status[4] == num:
            break
    print('time used {}'.format(time.time() - sta))
    numd = 0
    sumd = 0
    for i in range(num):
        if patient[i] == 1:
            numd += 1
            sumd += day - pat_day[i] - hea_day[i]
    pats = sum(patient)
    patd = sum(pat_day)
    deas = status[4]
    print('发病率：{}'.format(pats / num))
    print('无症状概率：{}'.format(sum(no_sym) / num))
    print('自愈率：{}'.format((num - pats) / num))
    print('治愈率：{}'.format(1 - deas / num))
    print('死亡率：{}'.format(deas / num))
    print('平均发病时间：{}'.format(patd / pats))
    print('平均治愈时间：{}'.format(sumd / numd))


if __name__ == '__main__':
    main()
    # test()
